package com.itheima.jdbc.数据库MYSQL.JDBC连接;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo3 {
    public static void main(String[] args) {
        Statement statement = null;
        Connection connection = null;
        //1.注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.定义sql id自动增长,使用null占位符
            String sql = "delete from account where id=3";
            //获取connection对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "nddwm");
            //4.获取执行sql的对象
            statement = connection.createStatement();
            //5.执行sql
            int i = statement.executeUpdate(sql);
            //处理结果
            System.out.println(i);
            if (i > 0) {
                System.out.println("删除成功!");
            } else {
                System.out.println("删除失败!");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            //避免空指针异常
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}
